using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Data;
using AppTableUtils;
using System;
using System.Text.RegularExpressions;


public class TranslatorManager : MonoBehaviour
{

    [Header("是否使用翻译")]
    public bool use_translate = true;

    [Header("当前语言")]
    public string language = "cn";
    string last_language;

    [Header("是否中文")]
    public bool is_cn = true;

    [Header("模板变量")]
    public Dictionary<string, object> template_variable_dc = new Dictionary<string, object>();
    
    
    public string translate_template(string template)
    {
        
        string res = BdUtils.replace_template_variables(template, template_variable_dc);
        return res;
    }
    
    // 切换使用语言
    public void btn_change_language()
    {
        is_cn = !is_cn;
    }
    
    public void btn_toggle_cn(bool toggle_cn = true)
    {
        is_cn = toggle_cn;
    }
    
    public void btn_set_language(string to_language)
    {
        language = to_language;
        is_cn = language == "cn" ? true : false;
        // is_cn = toggle_cn;
    }

    public Dictionary<string, TranslateText> translate_comp_dc = new Dictionary<string, TranslateText>();

    public static TranslatorManager instance;

    void Awake()
    {
        if (instance != null)
            Destroy(this);
        instance = this;
    }

    [Header("默认语言")]
    public string default_from_language = "cn";

    [Header("默认翻译为")]
    public string default_to_language = "";

    // --- 将输入的`text`从`from_language`转换为`to_language`
    public string translate_input_text_string(string text, string from_language = null, string to_language = null)
    {
        if (from_language == null)
            from_language = default_from_language;

        if (to_language == null)
            to_language = language;

        if (from_language == to_language)
            return text;

        string select = $"{from_language}='{text}'";
        var row = AppTableUtils.TableUtils.get(translator_dt, select: select);
        BdUtils.assert(row, $" *** text[{text}]` + from_language[{from_language}]对应`row`为空?");

        BdUtils.assert(translator_dt.Columns.Contains(to_language), $"`translator_dt`没有列`to_language[{to_language}]`");
        var _text = row[to_language];
        BdUtils.assert(_text, $" *** `text[{text}]`对应`to_language[{to_language}]`为空?");

        string translated_text = BdUtils.conv_to_string(_text);
        return translated_text;
    }

    [Header("code无对应时是否报错")]
    public bool flag__raise_error_if_code_map_none = false;
    public string get_translated_text_by_code(string code)
    {
        var row = AppTableUtils.TableUtils.get(translator_dt, code);
        var is_error = BdUtils.assert(row, $" *** `code[{code}]`对应`row`为空?", raise_error: flag__raise_error_if_code_map_none);
        if (is_error)
            return "";

        // BdUtils.log(" --- row:", TableUtils.conv_row_to_dc(row));
        // TableUtils.show_one_row(row);

        var _text = row[TranslatorManager.instance.language];
        // BdUtils.log(" --- _text:", _text);
        is_error = BdUtils.assert(_text, $" *** `code[{code}]`对应`language[{language}]`为空?", raise_error: false);
        if (is_error)
            return "";

        string translated_text = BdUtils.conv_to_string(_text);
        return translated_text;
    }

    [Header("csv文件路径")]
    public string csv_file_name = "translator.xlsx";

    DataTable _translator_dt;
    public DataTable translator_dt
    {
        get
        {
            if (_translator_dt == null)
            {
                // BdUtils.log("--- reload _translator_dt");
                // string f_path = BdUtils.join(dir_path, file_name);
                // _translator_dt = TableUtils.read_csv(f_path, primary_key: "ID");

                // _translator_dt = TableUtils.read_csv_data_on_all_platform(csv_file_name, primary_key: "code");
                // if (!csv_file_name.StartsWith("/"))
                // {
                //     // csv_file_name = BdUtils.join(BdUtils.data_table_dir_path, csv_file_name);
                //     //
                //     // // BdUtils.log("--- rel_sub_path:", BdUtils.get_relative_sub_path(BdUtils.streamingAssetsPath, BdUtils.data_table_dir_path));
                //     // // BdUtils.assert(BdUtils.exists_file(csv_file_name), $"表格文件不存在? csv_file_name: {csv_file_name}");
                //     // if (!BdTools.Flags.is_android)
                //     // {
                //     //     BdUtils.log("--- BdTools.Flags.is_android:", BdTools.Flags.is_android, "package:", BdPackage.BdTools.Flags.is_android);
                //     //     BdUtils.assert(BdUtils.exists_file(csv_file_name), $"表格文件不存在? csv_file_name: {csv_file_name}");
                //     // }
                // }
                // BdUtils.log($"--- csv_file_name: {csv_file_name}");
                _translator_dt = TableUtils.read_table(csv_file_name, primary_key: "code");
                
                // BdUtils.log("====== on load test_dt");
                // var test_dt = TableUtils.read_table("dialogs/level_1/dialog_tree_1.xlsx", primary_key: "id");
                // TableUtils.show_data_table(test_dt, zh_len: 20);
                
                // _translator_dt = TableUtils.read_table(csv_file_name, primary_key: "id");
                // _translator_dt = TableUtils.read_table(csv_file_name);

                // TableUtils.show_data_table(_translator_dt, zh_len: 20);
            }
            return _translator_dt;
        }
    }
    
    void Start()
    {
        BdUtils.call(translator_dt);
    }

    void Update()
    {
        if (is_cn)
            language = "cn";
        else
        {
            if (BdUtils.not_null(default_to_language))
                language = default_to_language;
        }

        if (use_translate)
            translate_all_text_comp();
    }

    public void translate_all_text_comp()
    {
        if (last_language == language)
            return;
        last_language = language;

        foreach (var kv in translate_comp_dc)
        {
            var value = kv.Value;
            value.translate_text();
        }
    }
}
